package com.zz.controller;

import com.zz.entity.User;
import com.zz.repository.UserRepository;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Random;

@RestController
public class UserController {
    @Resource
    UserRepository userRepository;
    @GetMapping("loginTest/{name}/{pwd}")
    public User loginTest(@PathVariable("name") String name,@PathVariable("pwd") String pwd){
        return  userRepository.findByNameAndPwd(name,pwd);
    }


    @GetMapping("save/{name}/{pwd}") // 网址 save/name/pwd
    private User saveUser(@PathVariable("name") String name,@PathVariable("pwd") String pwd){
        User user = new User();
        Random random = new Random();
        user.setId(random.nextInt());
        user.setName(name);
        user.setPwd(pwd);
        return userRepository.save(user);
    }
    @GetMapping("saveform") // saveform?id=xxx&name=xxx&pwd=xxx
    private User saveform(User user){
        return userRepository.save(user);
    }
}
